package com.yyk.yiliao.ui.mine.activity;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.ui.gongyong.activity.AddAdress_Activity;
import com.yyk.yiliao.ui.gongyong.activity.EditerAdress_Activity;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UseraddressList_Info;
import com.yyk.yiliao.widget.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的  ----- 设置 ------收获地址管理
 */
public class ShAdressGl2_Activity extends BaseActivity {

    @BindView(R.id.rv_shdz)
    RecyclerView rvShdz;
    @BindView(R.id.ll_center_bg)
    LinearLayout llCenterBg;
    @BindView(R.id.bt_addadress)
    Button btAddadress;

    private List<UseraddressList_Info.DataBean> data = new ArrayList<>();
    private BaseRecyclerAdapter<UseraddressList_Info.DataBean> adapter;


    /**
     * 重新初始化数据
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        initUseraddresslist(true);
    }


    @Override
    protected void onResume() {
        super.onResume();
        initUseraddresslist(true);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_sh_adress_gl2;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("收货地址管理");
    }

    @Override
    public void initData() {
        initAdapter();
    }

    private void initAdapter() {
        //收获管理条目
        adapter = new BaseRecyclerAdapter<UseraddressList_Info.DataBean>(mActivity, data, R.layout.adapter_item_shadressgl) {
            @Override
            public void convert(BaseRecyclerHolder holder, final UseraddressList_Info.DataBean item, final int position, boolean isScrolling) {
                ImageView img_editer = holder.getView(R.id.img_editer);
                img_editer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int id = item.getId(); //收货地址ID
                        Bundle bundle = new Bundle();
                        bundle.putString("id", id + "");
                        startAct(EditerAdress_Activity.class, bundle);
                    }
                });

                holder.setText(R.id.tv_name, item.getName());
                holder.setText(R.id.tv_phone, item.getPhone() + "");
                holder.setText(R.id.tv_address, item.getProvince_name() + item.getCity_name() + item.getCounty_name() + item.getAddress());
                int status = item.getStatus();
                if (status == 1) {
                    holder.getView(R.id.tv_status).setVisibility(View.GONE);
                } else {
                    holder.getView(R.id.tv_status).setVisibility(View.VISIBLE);

                }
            }


        };

        rvShdz.setLayoutManager(new LinearLayoutManager(mActivity));
        rvShdz.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_white_ef_1px, 0));
        rvShdz.setAdapter(adapter);
    }

    private void initUseraddresslist(final boolean falg) {
        Map<String, String> map = new TreeMap<>();

        map.put("uid", Hawk.get("uid", 0) + "");
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postUseraddresslist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<UseraddressList_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();


                    }

                    @Override
                    public void onNext(UseraddressList_Info info) {
                        Logger.e("收获管理地址" + info.toString());
                        if (info.getCode() == 1) {
                            llCenterBg.setVisibility(View.GONE);
                            rvShdz.setVisibility(View.VISIBLE);
                            if (falg) {
                                data.clear();
                            }
                            final List<UseraddressList_Info.DataBean> infoList = info.getData();
                            if (infoList.size() > 0 && infoList != null) {
                                data.addAll(infoList);
                            }
                            adapter.notifyDataSetChanged();

                        } else {
                            llCenterBg.setVisibility(View.VISIBLE);
                            rvShdz.setVisibility(View.GONE);
                        }
                    }
                });
    }

    @OnClick(R.id.bt_addadress)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //点击跳转新增地址
            case R.id.bt_addadress:
                startAct(AddAdress_Activity.class);
                break;
        }

    }



}
